﻿using Avalonia;
using System;
using System.IO;
using AvaloniaApplicationDemo.Util;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace AvaloniaApplicationDemo;

internal sealed class Program
{
    public static IServiceProvider ServiceProvider { get; private set; }

    // Initialization code. Don't use any Avalonia, third-party APIs or any
    // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
    // yet and stuff might break.
    [STAThread]
    public static void Main(string[] args)
    {
        var services = new ServiceCollection();
        // 在 services.AddServiceWithScrutor(); 之前添加，（这个应该可以AddServiceWithScrutor注册）


        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("Appsettings.json", false, true)
            .Build();
        if (configuration == null) throw new Exception("配置服务未注册，程序启动失败");

        services.AddSingleton<IConfiguration>(configuration);
        services.AddServiceWithScrutor();
        ServiceProvider = services.BuildServiceProvider();
        // 数据库初始化
        var connectionString = configuration.GetSection("DataBase")["ConnectionString"];
        if (string.IsNullOrEmpty(connectionString)) throw new Exception("数据库连接字符串配置不存在，程序启动失败");
        // 数据库初始化
        DataBaseManager.InitDbClient(connectionString);



        BuildAvaloniaApp()
            .StartWithClassicDesktopLifetime(args);
    }

    // Avalonia configuration, don't remove; also used by visual designer.
    public static AppBuilder BuildAvaloniaApp()
    {
        return AppBuilder.Configure<App>()
            .UsePlatformDetect()
            .WithInterFont()
            .LogToTrace();
    }
}